import { getGuid } from '@/utils'
import { cloneDeep } from 'lodash-es'

import type { BwCustomComStructModel } from '@bitwit/base-com-render'

/** 克隆节点元素 */
export function useCloneNodeInfo(data: BwCustomComStructModel) {
  /** 更新节点信息 */
  const updateNodeInfo = (info: BwCustomComStructModel | null) => {
    if (info) {
      const { children } = info

      const newNodeId = getGuid()
      info.nodeId = newNodeId
      if (children) {
        Object.values(children).forEach(
          (chideNodes: Array<BwCustomComStructModel>) => {
            if (Array.isArray(chideNodes) && chideNodes.length) {
              chideNodes.forEach((chide) => updateNodeInfo(chide))
            }
          }
        )
      }
    }
  }

  const newNodeEle: BwCustomComStructModel = cloneDeep(data)
  updateNodeInfo(newNodeEle)
  return newNodeEle
}
